﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using FlashGameSite.Manager;

namespace FlashGameSite
{
    /// <summary>
    /// Summary description for GameService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class GameService : System.Web.Services.WebService
    {
        [WebMethod]
        public string GetProfiles(string tag)
        {
            string result = string.Empty;
            using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
            {
                var queryResult = from obj in dataContext.UserInfos
                                  where obj.FullName.Contains(tag)
                                  select new {obj.UserID, obj.FullName };

                foreach (var item in queryResult)
                {
                    result += "{" + string.Format("\"UserID\":\"{0}\",\"Name\":\"{1}\"", item.UserID, item.FullName) + "}, ";
                }

                if (!string.IsNullOrEmpty(result))
                {
                    result = result.Remove(result.LastIndexOf(", "));
                }
            }

            return "[" + result + "]";
        }

        [WebMethod]
        public bool SendMessage(string input, string message)
        {
            bool result = false;

            try
            {
                using (FlashGamesDataContext dataContext = new FlashGamesDataContext())
                {
                    long fromUserId = Convert.ToInt64(Context.ApplicationInstance.Request.Cookies["fguid"].Value.Split('/')[0]);
                    dataContext.CreateNewMessage(Guid.NewGuid().ToString(), fromUserId, Convert.ToInt64(input), message);
                }

                result = true;
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }
    }
}
